import {
	config
} from "@/utils/config";
import store from '@/store/index.js';
// 例外不用token的地址
var exceptionAddrArr = [
	"/system/auth/login/applet/default",
	"/system/auth/login/applet/openid",
	"/system/auth/company/applet",
	"/applet/resource/applet/select",
	"/system/menu/market/route/tree",
	"/applet/jobsite/management/select",
	"/applet/commodity/general/sort",
	"/case/project/list",
	"/system/auth/get/verify/code"

];

// 校验token
function checkToken() {
	uni.request({
		url: config.publicBaseUrl + '/system/user/check/token',
		method: 'GET',
		header: {
			"content-type": "application/json",
			"Authorization": uni.getStorageSync('token')
		},
		success: (res) => {
			if (res.data.code == 200 || res.data.code == 1) {

			} else {
				// uni.showModal({
				// 	confirmColor: '#BF1111',
				// 	title: '账号异常提醒',
				// 	content: '你的账号已经异地登录,请重新登录',
				// 	showCancel: false
				// })
				commonhandle(res.data, true)
			}
		},
		fail: (err) => {
			reject(err);
		},
	});
}
//请求头处理函数
function createHeader(url) {
	let header = {
		"content-type": "application/json"
	};
	if (exceptionAddrArr.indexOf(url) == -1) {
		//排除请求的地址不须要token的地址
		let token = uni.getStorageSync('token');
		header.Authorization = token;
	}
	return header;
}
/**
 * 该方法实现请求的统一处理
 */
function commonhandle(resData, isShowToast) {
	console.log(resData, 'resData该方法实现请求的统一处理')
	// 名片未完善不抛异常
	if (resData.errCode != 1000012 && resData.errCode != 11001 && resData.errCode != 10030 && isShowToast) {
		uni.showToast({
			title: resData.msg ? resData.msg : resData.message,
			icon: "none",
			duration: 3000
		})
	}

	let codes = ['1002', '1001', '10001', '10006', 1002, 1001, 10001, 10006];
	if (codes.includes(resData.errCode)) {
		let pages = getCurrentPages() // 获取栈实例
		let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
		if (currentRoute !== 'my/loginIndex') {
			uni.reLaunch({
				url: '/my/loginIndex'
			});
		}
		//清除缓存
		uni.setStorageSync('token', '');
		uni.setStorageSync('userInfo', null);
		store.commit("userInfo", null);
		store.commit("isManager", null);
	}
}
export default class request {
	// 发起 GET 请求
	get(url, data, port, isShowToast = true) {
		let header = createHeader(url);
		let serverUrl = port == 'owner' ? config.ownerBaseUrl : port == 'project' ? config.projectBaseUrl : port ==
			'market' ? config.marketBaseUrl : config.publicBaseUrl;
		return new Promise((resolve, reject) => {
			uni.request({
				url: serverUrl + url,
				data,
				method: 'GET',
				header: header,
				success: (res) => {
					if (res.data.code == 200 || res.data.code == 1) {
						if (uni.getStorageSync('token') && url !==
							'/system/auth/login/applet/default' && url ==
							'/system/auth/login/applet/openid') {
							checkToken();
						}
						// console.log(res.data.data, 'res.data')
						resolve(res.data);
					} else {
						commonhandle(res.data, isShowToast)
						reject(res.data);
					}
				},
				fail: (err) => {
					reject(err);
				},
			});
		});
	}
	// 发起 POST 请求
	post(url, data, port, isShowToast = true) {
		let header = createHeader(url);
		let serverUrl = port == 'owner' ? config.ownerBaseUrl : port == 'project' ? config.projectBaseUrl : port ==
			'market' ? config.marketBaseUrl : config.publicBaseUrl
		return new Promise((resolve, reject) => {
			uni.request({
				url: serverUrl + url,
				data,
				method: 'POST',
				header: header,
				success: (res) => {
					if (res.data.code == 200 || res.data.code == 1) {
						res.data.isSuccess = true;
						if (uni.getStorageSync('token') && url !==
							'/system/auth/login/applet/default' && url ==
							'/system/auth/login/applet/openid') {
							checkToken();
						}
						resolve(res.data);
					} else {
						commonhandle(res.data, isShowToast)
						reject(res.data);
					}
				},
				fail: (err) => {
					reject(err);
				},
			});
		});
	}
	// 发起 PUT 请求
	put(url, data, port, isShowToast = true) {
		let header = createHeader(url);
		let serverUrl = port == 'owner' ? config.ownerBaseUrl : port == 'project' ? config.projectBaseUrl : port ==
			'market' ? config.marketBaseUrl : config.publicBaseUrl
		return new Promise((resolve, reject) => {
			uni.request({
				url: serverUrl + url,
				data,
				method: 'PUT',
				header: header,
				success: (res) => {
					if (res.data.code == 200 || res.data.code == 1) {
						res.data.isSuccess = true;
						if (uni.getStorageSync('token') && url !==
							'/system/auth/login/applet/default' && url ==
							'/system/auth/login/applet/openid') {
							checkToken();
						}
						resolve(res.data);
					} else {
						commonhandle(res.data, isShowToast)
						reject(res.data);
					}
				},
				fail: (err) => {
					reject(err);
				},
			});
		});
	}
	// 发起 PUT 请求
	delete(url, data, port, isShowToast = true) {
		let header = createHeader(url);
		let serverUrl = port == 'owner' ? config.ownerBaseUrl : port == 'project' ? config.projectBaseUrl : port ==
			'market' ? config.marketBaseUrl : config.publicBaseUrl
		return new Promise((resolve, reject) => {
			uni.request({
				url: serverUrl + url,
				data,
				method: 'DELETE',
				header: header,
				success: (res) => {
					if (res.data.code == 200 || res.data.code == 1) {
						res.data.isSuccess = true;
						if (uni.getStorageSync('token') && url !==
							'/system/auth/login/applet/default' && url ==
							'/system/auth/login/applet/openid') {
							checkToken();
						}
						resolve(res.data);
					} else {
						commonhandle(res.data, isShowToast)
						reject(res.data);
					}
				},
				fail: (err) => {
					reject(err);
				},
			});
		});
	}
}